package com.holyn.guofang.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.R;
import com.holyn.guofang.viewcommon.dialog.ToastDialog;
import com.holyn.mylibrary.LoadingDialog;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Holyn on 2017/7/14.
 */

public abstract class BaseFragment extends Fragment{
    Unbinder unbinder;
    private View rootView;

    private LoadingDialog loadingDialog;
    private ToastDialog toastDialog;

    private int countNum = 18;
    private Disposable countLoadingDisposable = null;

    protected abstract int getLayoutResID();
    protected abstract void initData();
    protected abstract void initView();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(getLayoutResID(), container, false);
        unbinder = ButterKnife.bind(this, rootView);

        initData();
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public View getRootView() {
        return rootView;
    }

    protected void showLoadingDialog(){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(getActivity());
        }
        loadingDialog.show();
    }

    protected void showLoadingDialog(String content){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(getActivity());
        }
        loadingDialog.setContent(content);
        loadingDialog.show();
    }

    protected void closeLoadingDialog(){
        if (loadingDialog != null){
            if (loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }
    }

    protected void showToastDialog(String content){
        if (toastDialog == null){
            toastDialog = new ToastDialog(getActivity());
        }
        toastDialog.setTvContent(content);
        toastDialog.show();
    }

    protected void closeToastDialog(){
        if (toastDialog != null){
            if (toastDialog.isShowing()){
                toastDialog.dismiss();
            }
        }
    }

    protected void toastMsg(String text){
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
    }
    //Toast的长时间显示
    protected void toastMsg(String text,int cnt){
        Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
    }

    /**
     *  展示加载的dialog，dialog的文字提示是不断增加的百分比
     */
    protected void showCountLoadingDialog(){
        if (loadingDialog == null){
            loadingDialog = new LoadingDialog(getActivity());
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
            loadingDialog.setContent("加载中18%...");
            startInterval();
        }
    }

    protected void closeCountLoadingDialog(){
        if (countLoadingDisposable != null){
            countLoadingDisposable.dispose();
        }
        if (loadingDialog != null){
            if (loadingDialog.isShowing()){
                loadingDialog.setContent("加载中100%...");
                countNum = 18;
                loadingDialog.dismiss();
            }
        }
    }

    /**
     *  启动百分比dialog的计时
     */
    protected void startInterval(){
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        countLoadingDisposable = disposable;
                    }
                }).subscribe(new Consumer<Long>() {
            @Override
            public void accept(@NonNull Long aLong) throws Exception {
                LogUtils.d("======> aLong:"+aLong);
                if (countNum < 99){
                    countNum++;
                    if (countNum%2 == 0) {
                        loadingDialog.setContent("加载中"+String.valueOf(countNum)+"%...");
                    } else {
                        loadingDialog.setContent("加载中"+String.valueOf(countNum)+"%....");
                    }
                }
            }
        });
    }
}
